package org.flexlancer.databases
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class dbServices extends EventDispatcher
	{
		private static const TABLE:String = "services";
		
		public function dbServices(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public static function List(condition:String = ""):Array{
			try{
				var serviceList:Array = DatabaseManager.Manager.Select(TABLE,null,condition);
				for each ( var singleService:Object in serviceList ){
					var serviceid:String = singleService.serviceid;
					var ppArray:Array = dbServicePrice.List("refid LIKE 'SERV-"+serviceid+"'");
					var completeprice:String = "";
					for each ( var singlePrice:Object in ppArray ){
						if ( completeprice != "" ) completeprice += ", ";
						completeprice += singlePrice.price;
					}
					singleService.completeprice = completeprice;
					singleService.prices = ppArray;
				}
				
				return serviceList;
			}catch(err:Error){
				throw err;
			}
			return null;
		}
		
		public static function AddService(serviceName:String, servicePrice:Array):void{
			try{//customerid INTEGER PRIMARY KEY AUTOINCREMENT","customername TEXT", "customertype TEXT", "credit REAL"]);
				DatabaseManager.Manager.Begin();
				var custObj:Object = new Object();
				custObj.servicename = " '" + serviceName + "' ";
				
				DatabaseManager.Manager.Insert(TABLE, custObj);
				var insertID:Number = DatabaseManager.Manager.LastInsertRowID;
				//var recs:Array = DatabaseManager.Manager.Select(TABLE);
				//insertID = recs[insertID-1].serviceid;
				
				if (servicePrice){
					dbServicePrice.Add(insertID,servicePrice);
				}
				
				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}

		public static function Update(serviceID:Number, serviceName:String, servicePrice:Array):void{
			try{//customerid INTEGER PRIMARY KEY AUTOINCREMENT","customername TEXT", "customertype TEXT", "credit REAL"]);
				DatabaseManager.Manager.Begin();
				var custObj:Object = new Object();
				custObj.servicename = " '" + serviceName + "' ";
				
				DatabaseManager.Manager.Update(TABLE, custObj, "serviceid = "+" '" + serviceID + "' ");
				//var insertID:Number = DatabaseManager.Manager.LastInsertRowID;
				//var recs:Array = DatabaseManager.Manager.Select(TABLE);
				//insertID = recs[insertID-1].serviceid;
				
				if (servicePrice){
					dbServicePrice.Delete("refid LIKE 'SERV-"+serviceID+"'");
					dbServicePrice.Add(serviceID,servicePrice);
				}
				
				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}


		public static function Delete(parmCategoryID:String):void{
			try{
				DatabaseManager.Manager.Delete(TABLE,"serviceid = "+parmCategoryID);
				dbServicePrice.Delete("refid LIKE 'SERV-"+parmCategoryID+"'");
			}catch(err:Error){
				throw err;
			}
		} 
		
	}
}